import { readFileSync } from 'fs-extra';
import { join } from 'path';
import { createApp, App } from 'vue';
import {  createBllScript, createGameComponentView, createModelScript, createScriptModule } from '../create-script';
import { TemplateGameComponent } from '../template/TemplateGameComponent';
import { TemplateModule } from '../template/TemplateModule';
import { TemplateModel } from '../template/TemplateModel';
import { TemplateBll } from '../template/TemplateBll';
import { TemplateView } from '../template/TemplateView';
import { TemplateViewMvvm } from '../template/TemplateViewMvvm';
const panelDataMap = new WeakMap<any, App>();
/**
 * @zh 如果希望兼容 3.3 之前的版本可以使用下方的代码
 * @en You can add the code below if you want compatibility with versions prior to 3.3
 */
// Editor.Panel.define = Editor.Panel.define || function(options: any) { return options }
module.exports = Editor.Panel.define({
    listeners: {
        show() { console.log('show'); },
        hide() { console.log('hide'); },
    },
    template: readFileSync(join(__dirname, '../../static/template/default/index.html'), 'utf-8'),
    style: readFileSync(join(__dirname, '../../static/style/default/index.css'), 'utf-8'),
    $: {
        app: '#app',
        text: '#text',
    },
    methods: {
        hello() {
            if (this.$.text) {
                this.$.text.innerHTML = 'hello';
                console.log('[cocos-panel-html.default]: hello');
            }
        },
    },
    ready() {
        var args = arguments as any;
        let title = " HiYOOO";
        let fileName = "Defailt";
        let showModule = false;
        let moduleName = "ModuleName";
        let path = args[0];
        let type = args[1];
        title = `i18n:hiyooo-plugin-framework.${type}`
        if (type == "createBll") {
            showModule = true;
        }
       
        if (this.$.app) {
            const app = createApp({});
            app.config.compilerOptions.isCustomElement = (tag) => tag.startsWith('ui-');
            app.component('MyCounter', {
                template: readFileSync(join(__dirname, '../../static/template/vue/create_file.html'), 'utf-8'),
                data() {
                    return {
                        title:title,
                        fileName:fileName,
                        showModule:showModule
                    };
                }, methods: {
                    onInputName(event:any){
                         
                        fileName = event.target.value;
                    },
                    onModuleName(event:any){
                        moduleName = event.target.value;
                    },
                    async onConfirm(){
                        // 检查文件名是否为空
                        if (fileName.trim().length == 0) {
                            await Editor.Dialog.info("i18n:hiyooo-plugin-framework.fileNameIsEmpty");
                            return;
                        }

                        // 检查文件名是否包含非法字符
                        if (fileName.match(/[\\/:*?"<>|]/)) {
                            await Editor.Dialog.info("i18n:hiyooo-plugin-framework.fileNameContainsInvalidCharacters");
                            return;
                        }

                        //创建对应文件
                      
                        switch (type) {
                            case "createGameComponent":
                                await createGameComponentView(path,fileName,TemplateGameComponent,false)
                                break;
                            case "createModule":
                                // await createModuleScript(path,fileName,"content");
                                await createScriptModule(path,fileName,TemplateModule);
                                break;
                            case "createModel":
                                await createModelScript(path,fileName,TemplateModel);
                                break;
                            case "createBll":
                                await createBllScript(path,fileName,TemplateBll,moduleName);
                                break;
                            case "createView":
                                await createGameComponentView(path,fileName,TemplateView)
                                break;
                            case "createMvvm":
                                await createGameComponentView(path,fileName,TemplateViewMvvm)
                                break;
                            default:
                                break;
                        }

                        close();

                    }
                },
            });
            app.mount(this.$.app);
            panelDataMap.set(this, app);
        }
    },
    beforeClose() { },
    close() {
        const app = panelDataMap.get(this);
        if (app) {
            app.unmount();
        }
    },
});
